package com.morenyang.webproject;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class Dao {
  private static final String DATABASE_URL = "jdbc:mysql://172.17.12.40:3306/test";
  private static final String USERNAME = "root", PASSWORD = "root";
  private static final String TABLE_NAME = "t_item";

  private DBHelper dbHelper = new DBHelper(DATABASE_URL, USERNAME, PASSWORD);

  public List<Item> getItemList() {
    String sql = "SELECT * FROM " + TABLE_NAME;
    List<Item> list = new ArrayList<>();
    try {
      ResultSet resultSet = dbHelper.getResult(sql);
      while (resultSet.next()) {
        Item item = new Item();
        item.setId(resultSet.getString("item_id"));
        item.setName(resultSet.getString("item_name"));
        item.setDetail(resultSet.getString("item_detail"));
        item.setPrice(resultSet.getDouble("item_price"));
        item.setNumber(resultSet.getInt("item_number"));
        list.add(item);
      }
      resultSet.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return list;
  }
}
